package com.ruoyi.common.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.domain.AOss;
import com.ruoyi.common.service.IAOssService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * oss配置管理Controller
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@RestController
@RequestMapping("/system/aOss")
public class AOssController extends BaseController {
    @Autowired
    private IAOssService aOssService;

    /**
     * 查询oss配置管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AOss aOss) {
        startPage();
        List<AOss> list = aOssService.selectAOssList(aOss);
        return getDataTable(list);
    }

    /**
     * 导出oss配置管理列表
     */
    @Log(title = "oss配置管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AOss aOss) {
        List<AOss> list = aOssService.selectAOssList(aOss);
        ExcelUtil<AOss> util = new ExcelUtil<AOss>(AOss.class);
        util.exportExcel(response, list, "oss配置管理数据");
    }

    /**
     * 获取oss配置管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(aOssService.selectAOssById(id));
    }

    /**
     * 新增oss配置管理
     */
    @Log(title = "oss配置管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AOss aOss) {
        return toAjax(aOssService.insertAOss(aOss));
    }

    /**
     * 修改oss配置管理
     */
    @Log(title = "oss配置管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AOss aOss) {
        return toAjax(aOssService.updateAOss(aOss));
    }

    /**
     * 删除oss配置管理
     */
    @Log(title = "oss配置管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            if (id == 1L) {
                return AjaxResult.error("本地存储不可删除");
            }
        }
        return toAjax(aOssService.deleteAOssByIds(ids));
    }

    /**
     * 获取oss默认配置
     */
    @GetMapping(value = "/current")
    public AjaxResult currentOss() {
        AOss currentOss = aOssService.getCurrentOss();
        AOss aOss = new AOss();
        aOss.setCurrentType(currentOss.getCurrentType());
        return success(aOss);
    }

    /**
     * 获取腾讯cos STS
     */
    @GetMapping("/cosSTS")
    public AjaxResult cosSTS() {
        return AjaxResult.success(aOssService.getCosSTS());
    }

    /**
     * 获取阿里oss STS
     * @return
     */
    @GetMapping("/oss")
    public AjaxResult ossSTS(){
        return AjaxResult.success(aOssService.getOssSTS());
    }

}
